<?php
include("../includes/global_files/tables.inc");
//include ("pagesettings/users.inc");

 require_once(DOCINCLUDES."global_files/global_pgsql.inc");
 $db = new cDatabase();
 $login_name = $_POST["login_name"];

// pass the login name and password entered by the user to the following query for authentication
 $login_name = $_POST["login_name"];
 $access_code1 = md5($_POST["access_code"]);

 $db->query = "select user_id from sys_master_user where login_name='$login_name' and access_code='$access_code1'";
 $db->runQuery();
 $authentication_details = $db->returnArrays();
 $user_id = $authentication_details[0]["user_id"];

// if authentication fails then throw an error message
 if (!$user_id) {
		 $msg = "Login Failed please try again!";
		 $msg = urlencode($msg);
		 $referring_url1 = explode('?',$_SERVER["HTTP_REFERER"]);
		 $referring_url = $referring_url1[0];
		 header("Location:$referring_url?msg=$msg");
		 exit;
 }

//if any sessions exist unset them and store new values in session variables
 session_start();
 unset($_SESSION);
 session_destroy();
 session_start();
 $_SESSION["guser_id"] = $user_id;
 $_SESSION["guser_name"] = $login_name;
 
 
 $db->query = "select smc.company_id, smc.company_name, scpd.product_category_id from $sys_master_company smc inner join   $sys_master_relationship smr on smr.partner_id=smc.company_id inner join $sys_user_info sui on sui.relationship_id = smr.relationship_id and sui.user_id='$user_id' inner join $sys_company_product_details scpd on scpd.relationship_id=smr.relationship_id and scpd.relationship_id=1";
 $db->runQuery();
 $user_company_array = $db->returnArrays();
// store the user details in the session
 $_SESSION["gcompany_id"] = $user_company_array[0]["company_id"];
 $_SESSION["guser_company_name"] = $user_company_array[0]["company_name"];
 $_SESSION["product_category_id"] = $user_company_array[0]["product_category_id"];

 
 $db->query = "select privilege_id, permission from sys_user_privilege where user_id='$user_id'";
 $db->runQuery();

 $user_privilege_arrays = $db->returnArrays();
 if ($user_privilege_arrays) {
		 foreach($user_privilege_arrays as $user_privilege_array) {
				 $privilege_id = $user_privilege_array["privilege_id"];
				 $_SESSION["guser_privilege"][$_SESSION["gcompany_id"]][$privilege_id] = $user_privilege_array["permission"];
		 }
 }
 
 $db->query = "select smp.preference_desc as preference,scp.preferred_values as value from sys_company_preferences scp join sys_master_preferences smp on smp.preference_id=scp.preference_id;";
 $db->runQuery();
 $user_preferences_arrays = $db->returnArrays();
 if ($user_preferences_arrays) {
		 for($i = 0,$cnt = count($user_preferences_arrays); $i < $cnt; $i++) {
				 $_SESSION["guser_preferences"][$user_preferences_arrays[$i]["preference"]] = $user_preferences_arrays[$i]["value"];
		 }
 }
 
 $db->query = "select language_id from sys_master_language sml, gen_user_preferences gup where sml.language_code=gup.preferred_values and gup.preference_id=10 and gup.user_id=$user_id";
 $db->runQuery();
 $user_language_array = $db->returnArrays();
 $_SESSION["user_language_id"] = $user_language_array[0]["language_id"];
 header("Location:../lrp/documents/gsp.php");
?>
